﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TitleHandSoapDispenserRepo
    {
        public const string m_msg_head = "Title HandSoapDispenser";

        public IEnumerable<TitleHandSoapDispenserModels> GetTitleHandSoapDispenser()
        {
            List<TitleHandSoapDispenserModels> TitleHandSoapDispensers = new List<TitleHandSoapDispenserModels>();
            string SQLCommand = @"select * from TBTitleHandSoapDispenser WHERE Status <> 'Delete' Order by TitleHandSoapDispenserId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleHandSoapDispenserModels TitleHandSoapDispenser = new TitleHandSoapDispenserModels();
                        TitleHandSoapDispenser.TitleHandSoapDispenserId = (int)reader["TitleHandSoapDispenserId"];
                        TitleHandSoapDispenser.TitleHandSoapDispenserName = (string)reader["TitleHandSoapDispenserName"];
                        TitleHandSoapDispenser.Status = (string)reader["Status"];

                        TitleHandSoapDispensers.Add(TitleHandSoapDispenser);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleHandSoapDispensers.ToList();
        }

        public TitleHandSoapDispenserModels GetTitleHandSoapDispenseById(int id)
        {
            TitleHandSoapDispenserModels TitleHandSoapDispenser = new TitleHandSoapDispenserModels();
            string SQLCommand = @"select * from TBTitleHandSoapDispenser where TitleHandSoapDispenserId=@TitleHandSoapDispenserId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@TitleHandSoapDispenserId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TitleHandSoapDispenser.TitleHandSoapDispenserId = (int)reader["TitleHandSoapDispenserId"];
                        TitleHandSoapDispenser.TitleHandSoapDispenserName = (string)reader["TitleHandSoapDispenserName"];
                        TitleHandSoapDispenser.Status = (string)reader["Status"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TitleHandSoapDispenser;
        }

        public bool Create(TitleHandSoapDispenserModels TitleHandSoapDispenser)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTitleHandSoapDispenser
                                    values (@TitleHandSoapDispenserName,@Status)";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleHandSoapDispenserName", TitleHandSoapDispenser.TitleHandSoapDispenserName);
                    db.AddParameter("@Status", "Active");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TitleHandSoapDispenserModels TitleHandSoapDispenser)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleHandSoapDispenser 
                                    Set TitleHandSoapDispenserName=@TitleHandSoapDispenserName
                                    where TitleHandSoapDispenserId=@TitleHandSoapDispenserId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleHandSoapDispenserId", TitleHandSoapDispenser.TitleHandSoapDispenserId);
                    db.AddParameter("@TitleHandSoapDispenserName", TitleHandSoapDispenser.TitleHandSoapDispenserName);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TitleHandSoapDispenserModels TitleHandSoapDispenser)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleHandSoapDispenser
                                    Set Status=@Status
                                    where TitleHandSoapDispenserId=@TitleHandSoapDispenserId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleHandSoapDispenserId", TitleHandSoapDispenser.TitleHandSoapDispenserId);
                    db.AddParameter("@Status", "Delete");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}